package com.ncs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ncs.entity.Result;
import com.ncs.pojo.Admin;
import com.ncs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/goToLogin")
    public Result goToLogin(@RequestBody Admin admin) {
        Result result = new Result();
        try {
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("username", admin.getUsername());
            wrapper.eq("password",admin.getPassword());
            Admin one = adminService.getOne(wrapper);
            if(one!=null) {
                result.setData(one);
                result.setFlag(true);
                result.setMessage("登录成功");
            }
            else {
                result.setFlag(false);
                result.setMessage("用户名或密码错误");
            }
        } catch (Exception e) {
            result.setFlag(false);
            result.setMessage("服务器繁忙");
        }

        return result;
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin) {
        Result result = new Result();

            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("username", admin.getUsername());
            wrapper.eq("password",admin.getPassword());
            Admin one = adminService.getOne(wrapper);
            if (one != null){
                result.setFlag(false);
                result.setMessage("存在相同的用户");
            }else {
//                Admin list = new Admin();
//                BeanUtil.copyProperties(list,one,true);
                adminService.save(admin);
                result.setData(admin);
                result.setFlag(true);
                result.setMessage("注册成功");
            }

        return result;
    }

}
